import librosa
import numpy as np

def preprocess_audio(file_path, sr=22050):
    try:
        # 加载音频文件
        y, sr = librosa.load(file_path, sr=sr)
        
        # 降噪
        y = librosa.effects.preemphasis(y)
        
        # 标准化
        y = np.float32(y) / np.max(np.abs(y))
        
        # 提取梅尔频谱图特征
        mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
        log_mel_spectrogram = librosa.power_to_db(mel_spectrogram)
        
        return log_mel_spectrogram.T  # 转置以匹配模型输入格式
    except Exception as e:
        print(f"Error processing {file_path}: {e}")
        return None


import jieba  # 假设使用中文，使用jieba进行分词
from sklearn.feature_extraction.text import TfidfVectorizer

def preprocess_text(text):
    # 分词
    words = jieba.lcut(text)
    
    # 去除停用词（示例）
    stopwords = set(['的', '了', '是', '在', '和', '有', '中', '上', '下', '不'])
    words = [word for word in words if word not in stopwords]
    
    # 返回处理后的文本
    return ' '.join(words)

# 假设我们有一个文本列表
texts = ["我今天很高兴", "我感到很悲伤", "他对我很生气"]

# 分词并去除停用词
preprocessed_texts = [preprocess_text(text) for text in texts]

# 使用TF-IDF进行向量化
vectorizer = TfidfVectorizer()
text_vectors = vectorizer.fit_transform(preprocessed_texts)

